<template>
  <div class="container">
    <!-- 表单筛选区域 -->
    <el-form inline>
      <el-form-item label="标签名称">
        <el-input
          v-model="queryData.tag_name"
          size="small"
          placeholder="请输入"
          maxlength="30px"
        />
      </el-form-item>
      <el-form-item label="归属学科">
        <el-select
          v-model="queryData.subject_id"
          placeholder="请选择学科"
          clearable
          maxlength="30px"
          @change="clear"
        >
          <el-option
            v-for="item in treeList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <!-- directoryList -->
      <el-form-item label="归属目录">
        <el-select
          v-model="queryData.directory_id"
          placeholder="请选择目录"
          clearable
          maxlength="30px"
        >
          <el-option
            v-for="item in directoryList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否显示">
        <el-select v-model="queryData.is_show" placeholder="是否显示">
          <el-option label="显示" value="1" />
          <el-option label="隐藏" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          size="small"
          @click="onSubmit"
        >搜索</el-button>
        <el-button size="small" @click="onReset">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 操作区域 -->
    <el-row type="flex" justify="space-between" align="middle">
      <!-- 左侧 -->
      <div class="left">
        <el-button
          type="primary"
          plain
          size="small"
          icon="el-icon-plus"
          @click="$refs.addForm.dialogShow = true"
        >新增</el-button>
      </div>
      <!-- 右侧 -->
      <div class="right">右侧区域</div>
    </el-row>
    <br>
    <!-- 表格区域 -->
    <el-table :data="list" border>
      <el-table-column type="index" label="#" />
      <el-table-column prop="tag_name" label="标签名称" />
      <el-table-column prop="directory_name" label="所属目录" />
      <el-table-column prop="subject_name" label="所归学科" />
      <el-table-column prop="username" label="创建人" />
      <el-table-column prop="is_show" label="是否显示" align="center">
        <template slot-scope="{ row }">
          <el-switch
            v-model="row.is_show"
            active-color="#13ce66"
            inactive-color="#ccc"
            :active-value="1"
            :inactive-value="2"
            active-text="是"
            inactive-text="否"
            @change="changeShow(row)"
          />
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="{ row }">
          <el-button
            type="success"
            plain
            size="small"
            icon="el-icon-edit"
            @click="editRow(row.id)"
          >修改</el-button>
          <el-button
            type="danger"
            plain
            size="small"
            icon="el-icon-delete"
            @click="delRow(row.id)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <br>
    <!-- 分页区域 -->
    <el-pagination
      :current-page.sync="queryData.page"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="queryData.limit"
      layout="total, sizes, prev, pager, next, jumper"
      :total="totalNum"
      background
      @size-change="sizeChange"
      @current-change="currentChange"
    />

    <!-- 新增、修改弹框区域 -->
    <addForm ref="addForm" @updateList="initData" />
  </div>
</template>
<script>
import {
  getTagListApi,
  getTagInfoApi,
  deleteTagListApi,
  isShowApi,
  getTreeList
} from '@/api/subject_tag'
import addForm from './components/tag_addForm.vue'
export default {
  name: 'Tag',
  components: {
    addForm
  },
  data() {
    return {
      list: [], // 表格数据
      totalNum: 0, // 总数
      // 列表的需要的参数
      queryData: {
        directory_id: '',
        subject_id: '',
        tag_name: '',
        is_show: '',
        page: 1,
        limit: 10
      },
      nowimg: [],
      treeList: []
    }
  },
  computed: {
    directoryList() { // 目录信息
      if (!this.queryData.subject_id) {
        return []
      } else {
        const result = this.treeList.find(val => val.id === this.queryData.subject_id)
        return result.children
      }
    }
  },

  async created() {
    await getTreeList()
    await this.initData()
    // console.log(1111, this.$router.history.current.query.subject_id)
    // await this.initData()
    // console.log(111)
    // console.log(111, this.$router.history.current.query)
    // if (this.$router.history.current.query.directory_id) {
    //   this.queryData.subject_id = this.$router.history.current.query.subject_id
    //   this.queryData.directory_id = this.$router.history.current.query.directory_id
    //   console.log(this.queryData)
    // }
    // 调用初始化
    // await this.initData()
  },

  methods: {
    clear() {
      this.queryData.directory_id = ''
    },
    async changeShow(row) {
      // console.log(rowId)
      const data = {
        id: row.id,
        is_show: row.is_show
      }
      await isShowApi(data)
      this.initData()
      this.$message.success('更新成功')
    },

    // 初始化数据
    async initData() {
      // 发起请求
      const data = await getTreeList()
      // if (this.$router.history.current.query.directory_id) {
      //   this.queryData.subject_id = this.$router.history.current.query.subject_id
      //   this.queryData.directory_id = this.$router.history.current.query.directory_id
      //   console.log(this.queryData)
      // }
      console.log(data)
      this.treeList = data.data
      this.$refs.addForm.treeList = data.data
      const res = await getTagListApi(this.queryData)
      // 数据可能需要加工
      // 赋值
      this.totalNum = res.data.total
      this.list = res.data.list
    },
    // 搜索提交
    onSubmit() {
      // 页码变成1
      this.queryData.page = 1
      // 拉取列表
      this.initData()
    },
    // 搜索重置
    onReset() {
      // 重置搜索表单
      this.queryData = {
        directory_id: '',
        subject_id: '',
        tag_name: '',
        is_show: '',
        page: 1,
        limit: 10
      }
      // 拉取列表
      this.initData()
    },
    // 每页数量变化的时候
    sizeChange(newSize) {
      // newSize 最新的每页数量
      this.queryData.limit = newSize
      this.initData()
    },
    // 页码变化事件
    currentChange(newPage) {
      // newPage 最新的页码
      this.queryData.page = newPage
      this.initData()
    },
    // 修改事件
    async editRow(rowId) {
      // 获取改行的数据
      const res = await getTagInfoApi(rowId)
      // 赋给弹框里面的表单
      // console.log(res);
      this.$refs.addForm.formData = res.data

      // 复杂点：一些负责的表单控件需要手动去回显
      // 打开弹框
      this.$refs.addForm.dialogShow = true
      await this.$nextTick()
    },
    // 删除事件
    async delRow(rowId) {
      // 弹出确认框
      await this.$confirm('确认删除吗')
      // 发生删除请求
      // await deleteSubjectListApi(rowId)
      await deleteTagListApi(rowId)
      // console.log({ id: rowId })
      console.log(rowId)
      // 提示成功
      this.$message.success('删除成功')
      // 边缘问题：如果不是第一页，且这一页只有一个，且删除成功，页码需要-1
      if (this.queryData.page > 1 && this.list.length === 1) {
        this.queryData.page--
      }
      // 列表更新
      this.initData()
    }
  }
}
</script>
<style scoped>
.container {
  padding: 30px;
}
</style>
